package com.twentytwok.hospitalsystem22K.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.twentytwok.hospitalsystem22K.bo.DepartmentBo;
import com.twentytwok.hospitalsystem22K.common.Code;
import com.twentytwok.hospitalsystem22K.common.R;
import com.twentytwok.hospitalsystem22K.mapper.DepartmentMapper;
import com.twentytwok.hospitalsystem22K.pojo.Department;
import com.twentytwok.hospitalsystem22K.pojo.DepartmentExample;
import com.twentytwok.hospitalsystem22K.service.DepartmentService;
import com.twentytwok.hospitalsystem22K.vo.DepartmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    private static List<Department> users = new ArrayList<>();

    @Override
    public R selectByParams(Map<String, Object> params) {
        PageHelper.startPage(Integer.parseInt(params.get("page") + ""), Integer.parseInt(params.get("limit") + ""));

        List<Department> departments = departmentMapper.selectByParams(params);

        PageInfo<Department> departmentPageInfo = new PageInfo<>(departments);

        R r = new R(Code.OK, "数据", (int) departmentPageInfo.getTotal(), departmentPageInfo.getList());

        return r;
    }

    @Override
    public R deleteDepartmentData(Integer id) {
        int result = departmentMapper.deleteByPrimaryKey(id);

        R r = new R();

        if (result == 1) {
            return new R(Code.OK, "删除成功");
        } else {
            return new R(Code.ERROR, "删除失败");
        }
    }

    @Override
    public R updateDepartmentData(Department department) {
        int result = departmentMapper.updateByPrimaryKeySelective(department);

        R r = new R();

        if (result == 1) {
            return new R(Code.OK, "修改成功");
        } else {
            return new R(Code.ERROR, "修改失败");
        }
    }

    @Override
    public R addDepartmentData(Department department) {
        int result = departmentMapper.insertSelective(department);

        if (result == 1) {
            return new R(Code.OK, "添加成功");
        } else {
            return new R(Code.ERROR, "添加失败");
        }
    }

    @Override
    public R deleteSelectedDepartmentData(List<Department> list) {
        DepartmentExample departmentExample = new DepartmentExample();

        List<Integer> list1 = new ArrayList<>();

        for (Department department : list) {
            list1.add(department.getId());
        }

        departmentExample.createCriteria().andIdIn(list1);

        int result = departmentMapper.deleteByExample(departmentExample);

        if (result > 0) {
            return new R(Code.OK, "删除成功");
        } else {
            return new R(Code.OK, "删除失败");
        }

    }

    @Override
    public List<DepartmentBo> selectDepartmentBarBo() {
        return departmentMapper.selectDepartmentNameBar();
    }

    @Override
    public DepartmentVo selectDepartmentBarVo() {
        List<DepartmentBo> departmentBos = selectDepartmentBarBo();

        List<String> departmentList = new ArrayList<>();
        List<Integer> numList = new ArrayList<>();

        DepartmentVo departmentVo = new DepartmentVo();

        departmentVo.setDepartmentList(departmentList);
        departmentVo.setNumList(numList);

        departmentBos.stream().forEach((bo) -> {
            String departmentName = bo.getDepartmentName();

            departmentVo.getDepartmentList().add(departmentName);

            Integer num = bo.getDoctorId();

            departmentVo.getNumList().add(num);

        });

        return departmentVo;
    }

    @Override
    public List<Department> selectAllDepartment() {
        return departmentMapper.selectByExample(null);
    }

}
